<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="model.Info"%>
<%@page import="connect.InfoDao"%>
<%@ page import="connect.HttpRequest" %>
<html>
<head>
    <title>DealShopping</title>
</head>
<body onload="confirmwindow()">
<script type="text/javascript" src="js/bootstrap.js"></script>

<%
    // 设置获取注册时的编码为UTF-8
    request.setCharacterEncoding("UTF-8");

    //创建交互层对象
    InfoDao infodao =new InfoDao();
    int trade=infodao.getCurrent()+1;
    String name = request.getParameter("name");
    String s = new String();
    //创建标志，看看获取是否成功
    boolean flag1=false,flag2=false,flag3=false,flag4=false;
    //获取purchase.jsp页面提交的数据
    if ((request.getParameter("thing1")).length()!=0){
        Integer amount = Integer.parseInt(request.getParameter("thing1"));
        Info info=new Info();
        info.setId(trade*10);
        info.setStatus(0);
        info.setPrice(amount*5);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";
        flag1=infodao.addInfo(info);
    }else {
        int amount=0;
        Info info=new Info();
        info.setId(trade*10);
        info.setStatus(0);
        info.setPrice(0);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";
        flag1=true;
    }

    if ((request.getParameter("thing2")).length()!=0){
        Integer amount = Integer.parseInt(request.getParameter("thing2"));
        Info info=new Info();
        info.setId(trade*10+1);
        info.setStatus(0);
        info.setPrice(amount*3);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";

        flag2=infodao.addInfo(info);
    }else{
        int amount=0;
        Info info=new Info();
        info.setId(trade*10+1);
        info.setStatus(0);
        info.setPrice(0);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";
        flag2=true;
    }
    if ((request.getParameter("thing3")).length()!=0){
        Integer amount = Integer.parseInt(request.getParameter("thing3"));
        Info info=new Info();
        info.setId(trade*10+2);
        info.setStatus(0);
        info.setPrice(amount*4);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";

        flag3=infodao.addInfo(info);
    }else {
        int amount=0;
        Info info=new Info();
        info.setId(trade*10+2);
        info.setStatus(0);
        info.setPrice(0);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";
        flag3=true;
    }
    if ((request.getParameter("thing4")).length()!=0){
        Integer amount = Integer.parseInt(request.getParameter("thing4"));
        Info info=new Info();
        info.setId(trade*10+3);
        info.setStatus(0);
        info.setPrice(amount*6);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";

        flag4 =infodao.addInfo(info);
    }else {
        int amount=0;
        Info info=new Info();
        info.setId(trade*10+3);
        info.setStatus(0);
        info.setPrice(0);
        info.setOrg(2);
        info.setName(name);
        s+=info.toJson(amount);
        s+=";";
        flag4=true;
    }
    if(!(flag1&&flag2&&flag3&&flag4)){
        response.sendRedirect("error.jsp");
    }
%>
<script>
    function confirmwindow(){
        <%
        String a =HttpRequest.sendGet("http://localhost:3000/callFabric/f3","l="+s);
        System.out.println(a);
        %>

        <%
           if(flag1&&flag2&&flag3&&flag4){
        %>
            alert('购买成功！');
            window.location.href="purchase1.jsp";
        <%
            }
            else
            {
                response.sendRedirect("error.jsp");
            }
        %>
    }
</script>
</body>
</html>
